var app = getApp();
const baseUrl = app.globalData.root;

const uploadMinio=  (url)=>{
    wx.showLoading();
    var token = wx.getStorageSync('token')
   return new Promise((resolve,reject)=>{
        wx.uploadFile({
            url: baseUrl+'/common/uploadMinio', // 仅为示例，非真实的接口地址
            filePath: url,
            header: {
                'Authorization': "Bearer "+token,
            },
            name: 'file',
            success(res) {
                wx.hideLoading();
               const data= JSON.parse(res.data);
              
             if(data.code==200){
                resolve(data)
             }else{
                 reject(data)
             }
            },
            fail(err){
                console.log(err
                  );
            }
          });
    })
    
}
const resultJudge = (res,type) =>{
    if(res.data.code==200){
        if(type=="list"){
            return res.data.rows;
        }
        if(type=="data"){
            return res.data.data
        }
        if(type=="none"){
            return res.data.code
        }
    }
    if(res.data.code==500){
    wx.showToast({
      title: res.msg,
     icon:"error"
    })
    return 
    }
}
// 调接口方法
const fetchData = (url, data, method, callback, errCallback) => {
    wx.showLoading();
    var token = wx.getStorageSync('token')
    wx.request({
        url: baseUrl + url,
        header: {
            'Authorization': "Bearer "+token,
        },
        method: method,
        data: data,
        success: function (res) {
            wx.hideLoading();
            // token过期，跳到授权页面
            if (res.data.code === 401) {

                wx.showModal({
                    title: "温馨提示", // 提示的标题
                    content: "超时登录，请重新登录", // 提示的内容
                    showCancel: false, // 是否显示取消按钮，默认true
                    cancelText: "取消", // 取消按钮的文字，最多4个字符
                    cancelColor: "#000000", // 取消按钮的文字颜色，必须是16进制格式的颜色字符串
                    confirmText: "确定", // 确认按钮的文字，最多4个字符
                    confirmColor: "#576B95", // 确认按钮的文字颜色，必须是 16 进制格式的颜色字符串
                    success: function (res) {
                        console.log("接口调用成功的回调函数");
                        if (res.confirm) {
                            wx.reLaunch({
                                url: '/pages/login/login'
                            })
                            console.log('用户点击确定')
                        } else if (res.cancel) {
                            console.log('用户点击取消')
                        }
                    },
                    fail: function () {
                        console.log("接口调用失败的回调函数");
                    },
                    complete: function () {
                        wx.setStorageSync('token', '')

                        console.log('超时登录，tocken失效')
                        console.log("接口调用结束的回调函数（调用成功、失败都会执行）");
                    }
                })
                return;
            }

            if (res.data.code !== 200 && res.data.code !== 0) {
                if (errCallback) {
                    return errCallback(res);
                }
               
                wx.showToast({
                    title: res.data.msg,
                    duration:2000,
                    icon: 'none'
                });
                return;
            }
            callback(res);
        }
    })
}
function getRequest(url, data = {}) {
    var token = wx.getStorageSync('token')
    return new Promise(function (resolve, reject) {
      wx.request({
        header: {
            'Authorization': "Bearer "+token,
        },
        method: "GET",
        url: baseUrl+url,
        data: data,
        success(res) {
          resolve(res.data);
        },
        fail(err) {
          reject(err.data)
        }
      })
    })
  }
function postRequest(url, data) {
    var token = wx.getStorageSync('token')
    return new Promise( (resolve, reject)=> {
      wx.request({
        header: {
            'Authorization': "Bearer "+token,
        },
        method: "POST",
        url: baseUrl+url,
        data: data,
        success:(res)=> {
          resolve(res.data);
        },
        fail:(err)=> {
          reject(err.data)
        }
      })
    })
  }
// 排除方法，在外面可调用
module.exports = {
    fetchData: fetchData,
    resultJudge:resultJudge,
    uploadMinio:uploadMinio,
    postRequest:postRequest,
    getRequest:getRequest
}